package com.dongdongshop.service.imp;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.BrandService;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.util.PageBean;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private TbGoodsMapper tbGoodsMapper;
    @Resource
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Resource
    private TbItemMapper tbItemMapper;
    @Resource
    private RocketMQTemplate rocketMQTemplate;
    @Reference
    private BrandService brandService;
    @Reference
    private ItemService itemService;

    @Override
    @Transactional
    public Integer addinfo(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, TbItem tbItem,String OptionString) {
        Integer i = tbGoodsMapper.insertSelective(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        Integer m = tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        Long categoryId = tbGoods.getCategory3Id();
        Long brandId = tbGoods.getBrandId();
        TbItemCat tbItemCat = itemService.getById(categoryId);
        TbBrand tbBrand = brandService.getById(brandId);

        //批量增加规格
        List<GoodsItemVo> list = JSONObject.parseArray(OptionString, GoodsItemVo.class);
        for (int j = 0; j < list.size(); j++) {
            String spec =  list.get(j).getSpec();
            tbItem.setSellPoint("畅销产品");
            tbItem.setBrand(tbBrand.getName());
            tbItem.setCategory(tbItemCat.getName());
            tbItem.setGoodsId(tbGoods.getId());
            tbItem.setNum(list.get(j).getNum());
            tbItem.setPrice(list.get(j).getPrice());
            tbItem.setSpec(spec);
            tbItemMapper.insertSelective(tbItem);
        }
        return m;
    }

    @Override
    @Transactional
    public PageBean<TbGoodsVo> getinfo(Integer number, Integer pageSize,String sname) {
        PageBean<TbGoodsVo> b = new PageBean<>();
        b.setPageNum(number);
        b.setPageSize(pageSize);
        List<TbGoodsVo> list = tbGoodsMapper.getinfo(b,sname);
        List<TbGoodsVo> slist = tbGoodsMapper.getCount(sname);
        b.setResult(list);
        b.setTotal(slist.size());
        return b;
    }

    @Override
    public Integer deleteAll(Long[] ids) {
        return tbGoodsMapper.deleteAll(ids);
    }

    @Override
    public Integer getSuccess(Long[] ids) {
        Integer i = tbGoodsMapper.getSuccess(ids);
        if (i>0){
            rocketMQTemplate.convertAndSend("getSuccess",ids);
        }
        return i;
    }

    @Override
    public Integer getBack(Long[] ids) {
        return tbGoodsMapper.getBack(ids);
    }

    @Override
    public PageBean<TbGoodsVo> getinfoSeller(Integer number, Integer pageSize, String status, String sname,String sellerId) {
        PageBean<TbGoodsVo> b = new PageBean<>();
        b.setPageNum(number);
        b.setPageSize(pageSize);
        List<TbGoodsVo> list = tbGoodsMapper.getinfoSeller(b,sname,status,sellerId);
        List<TbGoodsVo> slist = tbGoodsMapper.getCountSeller(sname,status,sellerId);
        b.setResult(list);
        b.setTotal(slist.size());
        return b;
    }

    @Override
    public Integer goGo(Long[] ids) {
        return tbGoodsMapper.toGo(ids);
    }

    @Override
    public TbGoods getById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public TbGoodsVo getItemName(Long goodsId) {
        return tbGoodsMapper.getItemName(goodsId);
    }
}
